/*
 * JTip
 * By Cody Lindley (http://www.codylindley.com)
 * Under an Attribution, Share Alike License
 * JTip is built on top of the very light weight jquery library.
 */

//on page load (as soon as its ready) call JT_init
$(document).ready(JT_init);

function JT_init(){
	$("a.jTip")
		   .click(function(){$('#JT').remove();JT_show(this.href,this.id,this.name)});
}
//关闭弹窗
function jt_close(){
	$('#JT').remove();
	$('.rb_mask').remove();
}
//创建新部门
var selectDepName,selectDepId;
function JT_createDep(linkId,title){	
	if(title == false)title="&nbsp;";
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - 3; //set y position
	var params = 350;
	var height = 350;
	$("body").append("<div class='rb_mask'></div><div id='JT' style='width:"+params+"px;height:"+height+"px'><div id='JT_arrow_right' style='left:"+(params+1)+"px'></div><div id='JT_close_right'>"+title+"<div id='JT_close_btn' onclick='jt_close()'></div></div><div id='JT_copy'><div class='JT_loader'><div>请选择上级部门:<div class='JT_from_list' id='depList'><p>加载中...</p></div><div class='JT_from'><img src='assets/top_input_icon_search.png'/><input type='text' placeholder='输入要创建的部门名...' name='name' id='createDepName' /><input type='button' value='创建' onclick='createDep()'></div></div></div>");
	var clickElementx = getAbsoluteLeft(linkId) - (params + 20);	
	
	$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#JT').show();
	
	
	getDepListNoTreeData();
	//$('#JT_copy').load(url);
	
	

}

//修改部门
function JT_editDep(linkId,title){
	selectDepId = 0;
	if(title == false)title="&nbsp;";
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - 3;
	var params = 350;
	var height = 350;
	if (hasArea>(BODYWIDTH-params)/2){
		$("body").append("<div class='rb_mask'></div><div id='JT' style='width:"+params+"px;height:"+height+"px'><div id='JT_arrow_left'></div><div id='JT_close_left'>"+title+"<div id='JT_close_btn' onclick='jt_close()'></div></div><div id='JT_copy'><div class='JT_loader'><div>请选择新的上级部门:<div class='JT_from_list' id='depList'><p>加载中...</p></div><div class='JT_from'><img src='assets/top_input_icon_search.png'/><input type='text' placeholder='输入新的部门名...' name='editDepName' id='editDepName' /><input name='sumbit' type='button' value='修改' onClick='editDep(\""+linkId+"\")'></div></div></div>");
		var arrowOffset = getElementWidth(linkId) + 16;
		var clickElementx = getAbsoluteLeft(linkId) + arrowOffset; //set x position
	}else{
		$("body").append("<div class='rb_mask'></div><div id='JT' style='width:"+params+"px;height:"+height+"px'><div id='JT_arrow_right' style='left:"+params+"px'></div><div id='JT_close_left'>"+title+"<div id='JT_close_btn' onclick='jt_close()'></div></div><div id='JT_copy'><div class='JT_loader'><div>请选择新的上级部门:<div class='JT_from_list' id='depList'><p>加载中...</p></div><div class='JT_from'><img src='assets/top_input_icon_search.png'/><input type='text' placeholder='输入新的部门名...' name='editDepName' id='editDepName' /><input name='sumbit' type='button' value='修改' onClick='editDep(\""+linkId+"\")'></div></div></div>");//right side
		var arrowOffset = getElementWidth(linkId) - 10;
		var clickElementx = getAbsoluteLeft(linkId) - arrowOffset - params; //set x position
	}
	
	getDepListNoTreeData(linkId);
	
	$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#JT').show();
	$("#editDepName").val($("#"+linkId).parent().find("span").text());
	JTDrop();
	//$('#JT_copy').load(url);
	
	

}

//创建新角色
function JT_createRole(linkId,title){
	if(title == false)title="&nbsp;";
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - 3; //set y position
	var params = 350;
	var height = 150;
	$("body").append("<div class='rb_mask'></div><div id='JT' style='width:"+params+"px;height:"+height+"px'><div id='JT_arrow_right' style='left:"+(params+1)+"px'></div><div id='JT_close_right'>"+title+"<div id='JT_close_btn' onclick='jt_close()'></div></div><div id='JT_copy'><div class='JT_loader'><div><div class='JT_from'><img src='assets/top_input_icon_search.png'/><input type='text' placeholder='输入要创建的角色名...' name='createRoleName' id='createRoleName' /></div><div class='JT_from'><img src='assets/top_input_icon_search.png'/><input type='text' placeholder='输入要创建的角色说明...' name='createRoleEtc' id='createRoleEtc' /><input name='sumbit' type='button' value='创建' onClick='createRole()'></div></div></div>");
	var clickElementx = getAbsoluteLeft(linkId) - (params + 20);
	
	$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#JT').show();
	$(".JT_loader").mCustomScrollbar();
	$(".JT_loader div").css("overflow", "hidden");
	//$('#JT_copy').load(url);

}
//修改角色名
function JT_editRoleName(linkId,title,roleId){
	var name = $("#"+roleId+" > td:first-child").text();
	var text = $("#"+roleId+" > td:nth-child(2)").text();
	if(title == false)title="&nbsp;";
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - 3; //set y position
	var params = 350;
	var height = 210;
	$("body").append("<div class='rb_mask'></div><div id='JT' style='width:"+params+"px;height:"+height+"px'><div id='JT_arrow_right' style='left:"+(params+1)+"px'></div><div id='JT_close_right'>"+title+"<div id='JT_close_btn' onclick='jt_close()'></div></div><div id='JT_copy'><div class='JT_loader'><div><p><b>新的角色名:</b></p><div class='JT_from'><img src='assets/top_input_icon_search.png'/><input type='text' placeholder='输入要修改的角色名...' name='editRoleName' id='editRoleName' value='"+name+"' /></div><p><b>新的角色说明:</b></p><div class='JT_from'><img src='assets/top_input_icon_search.png'/><input type='text' placeholder='输入要修改的角色说明...' name='editRoleEtc' id='editRoleEtc' value='"+text+"'/><input name='sumbit' type='button' value='修改' onClick='editRole(\""+roleId+"\")'></div></div>");
	var clickElementx = getAbsoluteLeft(linkId) - (params + 20);
	
	$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#JT').show();
	$(".JT_loader").mCustomScrollbar();
	$(".JT_loader div").css("overflow", "hidden");
	JTDrop();
	//$('#JT_copy').load(url);

}
//创建新人员
function JT_createUser(linkId,title){	
	if(title == false)title="&nbsp;";
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - 3; //set y position
	var params = 350;
	var height = 570;
	$("body").append("<div class='rb_mask'></div><div id='JT' style='width:"+params+"px;height:"+height+"px'><div id='JT_arrow_right' style='left:"+(params+1)+"px'></div><div id='JT_close_right'>"+title+"<div id='JT_close_btn' onclick='jt_close()'></div></div><div id='JT_copy'><div class='JT_loader'><div><p>请选择人员部门(单选):</p><div class='JT_from_list' id='depList'><p>加载中...</p></div></div><div><p>请选择人员角色(多选):</p></div><div class='JT_from_list' id='roleList'><p>加载中...</p></div></div><div class='JT_from'><img src='assets/top_input_icon_search.png'/><input type='text' placeholder='用户名...' id='createUserName' />	</div><div class='JT_from'><img src='assets/top_input_icon_search.png'/><input type='text' placeholder='真实姓名...' id='createRealName' /></div><div class='JT_from'><img src='assets/top_input_icon_search.png'/><input type='text' placeholder='邮件地址...' id='createEmail' /><input name='sumbit' type='button' value='创建' onClick='createUser()'></div></div></div></div>");
	var clickElementx = getAbsoluteLeft(linkId) - (params + 20);	
	
	//获取角色列表
	getRoleList();
	
	$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#JT').show();	
	$("#depList").css("overflow", "hidden");
	$("#depList").css("height", "140px");
	$("#roleList").css("overflow", "hidden");
	$("#roleList").css("height", "140px");
	selectDepId = -1;
	//$('#JT_copy').load(url);	

}
//修改人员信息
function JT_editUser(linkId,title,roleArr,depId){	
	if(title == false)title="&nbsp;";
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - 3; //set y position
	var params = 350;
	var height = 570;
	var userId = linkId.substring(5,linkId.length);
	$("body").append("<div class='rb_mask'></div><div id='JT' style='width:"+params+"px;height:"+height+"px'><div id='JT_arrow_right' style='left:"+(params+1)+"px'></div><div id='JT_close_right'>"+title+"<div id='JT_close_btn' onclick='jt_close()'></div></div><div id='JT_copy'><div class='JT_loader'><div><p>请选择人员部门(单选):</p><div class='JT_from_list' id='depList'><p>加载中...</p></div></div><div><p>请选择人员角色(多选):</p></div><div class='JT_from_list' id='roleList'><p>加载中...</p></div></div><div class='JT_from'><img src='assets/top_input_icon_search.png'/><input type='text' placeholder='用户名...' id='editUserName' />	</div><div class='JT_from'><img src='assets/top_input_icon_search.png'/><input type='text' placeholder='真实姓名...' id='editRealName' /></div><div class='JT_from'><img src='assets/top_input_icon_search.png'/><input type='text' placeholder='邮件地址...' id='editEmail' /><input name='sumbit' type='button' value='修改' onClick='editUser(\""+userId+"\");' /></div></div></div></div>");
	var clickElementx = getAbsoluteLeft(linkId) - (params + 20);	
	
	//获取角色列表
	getRoleList(roleArr,depId);
	$("#editUserName").val($("#"+linkId).parent().parent().find("td:nth-child(3)").text());
	$("#editRealName").val($("#"+linkId).parent().parent().find("td:nth-child(2)").text());
	$("#editEmail").val($("#"+linkId).parent().parent().find("input[type='hidden']").val());
	//alert($("#"+linkId).parent().parent().find("td:nth-child(2)").text());
	
	$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#JT').show();	
	$("#depList").css("overflow", "hidden");
	$("#depList").css("height", "140px");
	$("#roleList").css("overflow", "hidden");
	$("#roleList").css("height", "140px");
	selectDepId = -1;
	JTDrop();
	//$('#JT_copy').load(url);	

}
//修改步骤名
function JT_editFlowName(linkId,title,roleId){
	if(title == false)title="&nbsp;";
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - 3; //set y position
	var params = 350;
	var height = 150;
	$("body").append("<div class='rb_mask'></div><div id='JT' style='width:"+params+"px;height:"+height+"px'><div id='JT_arrow_right' style='left:"+(params+1)+"px'></div><div id='JT_close_right'>"+title+"<div id='JT_close_btn' onclick='jt_close()'></div></div><div id='JT_copy'><div class='JT_loader'><div><div class='JT_from'><img src='assets/top_input_icon_search.png'/><input type='text' placeholder='输入要修改的步骤名...' name='editFlowName' id='editFlowName' /><input name='sumbit' type='button' value='修改' onClick='editFlowName(\""+roleId+"\")'></div></div>");
	var clickElementx = getAbsoluteLeft(linkId) - (params + 20);
	
	$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#JT').show();
	$(".JT_loader").mCustomScrollbar();
	$(".JT_loader div").css("overflow", "hidden");
	$('#JT_copy').load(url);

}


function JT_show(url,linkId,title){
	if(title == false)title="&nbsp;";
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - 3; //set y position
	
	var queryString = url.replace(/^[^\?]+\??/,'');
	var params = parseQuery( queryString );
	if(params['width'] === undefined){params['width'] = 350};
	if(params['link'] !== undefined){
	$('#' + linkId).bind('click',function(){window.location = params['link']});
	$('#' + linkId).css('cursor','pointer');
	}
	if(hasArea>((params['width']*1)+75)){
		$("body").append("<div class='rb_mask'></div><div id='JT' style='width:"+params['width']*1+"px'><div id='JT_arrow_left'></div><div id='JT_close_left'>"+title+"<div id='JT_close_btn' onclick='jt_close()'></div></div><div id='JT_copy'><div class='JT_loader'><div><p><a href='javascript:reload(xxx)'>点击刷新</a></p><p>内容在这里</p><p>内容在这里</p><p>内容在这里</p><p>内容在这里</p><p>内容在这里</p><p>内容在这里</p><p>内容在这里</p><p>内容在这里</p><p>内容在这里</p></div></div>");//right side
		var arrowOffset = getElementWidth(linkId) + 16;
		var clickElementx = getAbsoluteLeft(linkId) + arrowOffset; //set x position
	}else{
		$("body").append("<div class='rb_mask'></div><div id='JT' style='width:"+params['width']*1+"px'><div id='JT_arrow_right' style='left:"+((params['width']*1)+1)+"px'></div><div id='JT_close_right'>"+title+"<div id='JT_close_btn' onclick='jt_close()'></div></div><div id='JT_copy'><div class='JT_loader'><div><p><a href='javascript:reload(xxx)'>点击刷新</a></p><p>内容在这里</p></div></div>");//left side
		var clickElementx = getAbsoluteLeft(linkId) - ((params['width']*1) + 20); //set x position
	}
	
	$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#JT').show();
	$(".JT_loader").mCustomScrollbar();
	$(".JT_loader div").css("overflow", "hidden");
	$('#JT_copy').load(url);

}

function getElementWidth(objectId) {
	x = document.getElementById(objectId);
	return x.offsetWidth;
}

function getAbsoluteLeft(objectId) {
	// Get an object left position from the upper left viewport corner
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	return oLeft
}

function getAbsoluteTop(objectId) {
	// Get an object top position from the upper left viewport corner
	o = document.getElementById(objectId)
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	return oTop
}

function parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

function blockEvents(evt) {
              if(evt.target){
              evt.preventDefault();
              }else{
              evt.returnValue = false;
              }
}

//JT可拖拽
function JTDrop(){
	var jt_x,jt_y = 0;
	$("#JT_close_left,#JT_close_right").mouseover(function(){
		$(this).css("cursor", "move"); //改变鼠标悬停指针的形状
	});
	$("#JT_close_left,#JT_close_right").mousedown(function (e) { //e鼠标事件
	
		$("#JT").css("cursor", "move"); //改变鼠标指针的形状
	
		var offset = $(this).offset(); //DIV在页面的位置
		var x = e.pageX - offset.left; //获得鼠标指针离DIV元素左边界的距离
		var y = e.pageY - offset.top; //获得鼠标指针离DIV元素上边界的距离
		$(document).bind("mousemove", function (ev)//绑定鼠标的移动事件，因为光标在DIV元素外面也要有效果，所以要用doucment的事件，而不用DIV元素的事件
		{
			$("#JT").stop(); //加上这个之后
	
			jt_x = ev.pageX - x; //获得X轴方向移动的值
			jt_y = ev.pageY - y; //获得Y轴方向移动的值
	
			$("#JT").animate({ left: jt_x + "px", top: jt_y + "px" }, 10);
		});
		
		$(document).bind("mouseup", function (ev)//绑定鼠标的移动事件，因为光标在DIV元素外面也要有效果，所以要用doucment的事件，而不用DIV元素的事件
		{
			$("#JT").css("cursor", "default");
			$(this).unbind("mousemove");
		});
	
	});
}